
import { _decorator, Component, Node, RigidBody, tween, Tween, Vec3 } from 'cc';
import { GameMgr } from './GameMgr';
const { ccclass, property } = _decorator;
/**
 * Predefined variables
 * Name = Spike
 * DateTime = Mon Mar 07 2022 17:18:30 GMT+0800 (中国标准时间)
 * Author = lamd
 * FileBasename = Spike.ts
 * FileBasenameNoExtension = Spike
 * URL = db://assets/Scripts/Spike.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/en/
 *
 */

@ccclass('Spike')
export class Spike extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;
    moveNode: Node = null;

    dir: number = 1;
    stopTime: number = 0;
    start() {
        this.moveNode = this.node.getChildByName("moving_spikes-b");

        // let t: Tween<Node> = tween(moveNode)
        // let t1: Tween<Node> = t.by(2, { worldPosition: })
        // let t2: Tween<Node> = t.delay(1)
        // let t3: Tween<Node> = t.by(2, { worldPosition: new Vec3(0, -0.15, 0) })
        // t.repeatForever(t.sequence(t1, t2, t3)).start(); // 只能重复5次？
        this.dir = 1;
        this.stopTime = 2;
    }

    update(deltaTime: number) {
        if(GameMgr.instance.isGameOver) return;
        
        this.stopTime += deltaTime;
        if (this.stopTime < 2) // 暂停时间
            return;
            
        let pos: Vec3 = this.moveNode.position;
        if (this.dir == 1 && pos.z >= 0.16){   // 向上时转向
            this.stopTime = 0
            this.dir = -1;
        }
        else if (this.dir == -1 && pos.z <= 0){
            this.dir = 1;
            this.stopTime = 0
        }
        // let outPos: Vec3 = new Vec3();
        // Vec3.lerp(outPos, pos, this.target, timeScale * deltaTime);

        pos.z += 0.2 * deltaTime * this.dir;
        this.moveNode.position = pos;
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/en/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/en/scripting/life-cycle-callbacks.html
 */
